package com.fwj.common.web;

import com.alibaba.fastjson.JSONObject;
import com.fwj.common.annotation.Files;
import com.fwj.common.api.AttachmentService;
import com.fwj.common.entity.Attachment;
import com.fwj.common.annotation.DownloadPermission;
import com.fwj.core.common.model.ResponseDTO;
import com.fwj.core.common.model.ResponseEnum;
import com.fwj.core.common.annotation.LoginUser;
import com.fwj.user.entity.user.UserBaseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: fwj
 * @Data: 2022/4/15 5:56
 * @Version 1.0
 * @Description:
 */

@RestController
@RequestMapping("/attachment")
@Validated
public class AttachmentController {

    @Autowired
    private AttachmentService attachmentService;

    /**
     * @Description 附件上传接口
     * @Param: files    附件数组
     * @Param: scope    附件类型
     * @Param: attachNo 附件ID
     * @Author fwj
     * @Createtime 2022/4/15
     */
    @PostMapping(value = "upload")
    @Files
    public ResponseDTO upload(@RequestParam("files") MultipartFile[] files,
                              @RequestParam(required = false, value = "scope") String scope,
                              @RequestParam(required = false, value = "attachNo") String attachNo,
                              @RequestParam(required = false, value = "description") String description,
                              @RequestParam(required = false, value = "isSecure") String isSecure,
                              @LoginUser UserBaseInfo userBaseInfo){
        ResponseDTO responseDTO = new ResponseDTO();
        try{
            //封装包装对象
            Attachment attachment = buildAttach(scope,attachNo,description,isSecure);
            return attachmentService.upload(files, attachment,userBaseInfo);
        } catch (Exception e){
            return responseDTO.fail(ResponseEnum.SERVER_ERROR);
        }
    }

    private Attachment buildAttach(String scope, String attachNo, String description, String isSecure) {
        //封装包装对象
        Attachment attachment = new Attachment();
        attachment.setScope(scope);
        attachment.setDescription(description);
        attachment.setAttachNo(attachNo);
        attachment.setIsSecure(isSecure);
        return attachment;
    }

    /**
     * @Description 附件下载接口
     * @Param: jsonObject   根据attachNo下载附件，JSON格式
     * @Author fwj
     * @Createtime 2022/4/15
     */
    @GetMapping(value = "download")
    public ResponseDTO download(@RequestParam("attachNo") String attachNo, HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        try{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("attachNo",attachNo);
            return attachmentService.download(jsonObject,request,response);
        } catch (Exception e){
            return responseDTO.fail(ResponseEnum.SERVER_ERROR);
        }
    }

    /**
     * @Description 附件下载接口
     * @Param: jsonObject   根据attachNo下载附件，JSON格式
     * @Author fwj
     * @Createtime 2022/4/15
     */
    @PostMapping(value = "download")
    public ResponseDTO download(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo, HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        try{
            return attachmentService.download(jsonObject,request,response);
        } catch (Exception e){
            return responseDTO.fail(ResponseEnum.SERVER_ERROR);
        }
    }

    /**
     * @Description 安全附件下载接口
     * @Param: jsonObject   根据attachNo下载附件，JSON格式
     * @Author fwj
     * @Createtime 2022/4/15
     */
    @PostMapping(value = "secureDownload")
    @DownloadPermission
    public ResponseDTO secureDownload(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo,HttpServletRequest request, HttpServletResponse response){
        ResponseDTO responseDTO = new ResponseDTO();
        try{
            return attachmentService.download(jsonObject,request,response);
        } catch (Exception e){
            return responseDTO.fail(ResponseEnum.SERVER_ERROR);
        }
    }


    /**
     * @Description 附件删除接口,伪删除，标记附件状态为FalseDeleted
     * @Param: jsonObject   根据attachNo删除附件，JSON格式
     * @Author fwj
     * @Createtime 2022/4/15
     */
    @PostMapping(value = "delete")
    public ResponseDTO delete(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        ResponseDTO responseDTO = new ResponseDTO();
        try{
            return attachmentService.delete(jsonObject);
        } catch (Exception e){
            return responseDTO.fail(ResponseEnum.SERVER_ERROR);
        }
    }

    /**
     * @Description 附件真实删除接口，附件状态为FalseDeleted，暂时禁用
     * @Param: jsonObject   根据attachNo删除附件，JSON格式
     * @Author fwj
     * @Createtime 2022/4/15
     */
    @PostMapping(value = "realDelete")
    public ResponseDTO realDelete(@RequestBody JSONObject jsonObject, @LoginUser(Roles = "Admin") UserBaseInfo userBaseInfo){
        ResponseDTO responseDTO = new ResponseDTO();
        try{
            return responseDTO.fail();
//            JSONArray jsonArray =  jsonObject.getJSONArray("delList");
//            List<String> keys = JSONObject.parseArray(jsonArray.toJSONString(),String.class);
//            return attachmentService.realDelete(keys);
        } catch (Exception e){
            return responseDTO.fail(ResponseEnum.SERVER_ERROR);
        }
    }
}
